package cn.ecut.ajax;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lujiapeng
 * @className JQueryAjaxServlet
 * @description TODO
 * @date 2020/9/17 8:10
 **/
@WebServlet("/jQueryAjax")
public class JQueryAjaxServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Enumeration<String> parameterNames = request.getParameterNames();
        while(parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String value = request.getParameter( name );

            System.out.println( name + " : " + value );
        }
        // 生成响应
        Map<String , Object> map = new HashMap<>() ;
        map.put("message" , "成功") ;
        map.put("data" , "我是数据") ;
        map.put( "status" , 0 ) ;

        String jsonString = JSON.toJSONString(map);
        response.getWriter().write( jsonString );
    }
}
